//logs.js
var util = require('../../utils/util.js')
Page({
  data: {
    item: {
       display    :'hide',
       error_info :'操作太过频繁',
     },
    battle_id   : '',
    player1_name:'丁 先生',
    player2_name:'奥 先生',
    player1_score:'000',
    player2_score:'000',
    sum_frame:0,
    last_time    :'',
    event_name   :'赛事名称',
    event_stage  :'比赛阶段'
  },
  //获取对阵的信息
  onShow:function(){
      var battle_info = wx.getStorageSync('battle_info') || [];
      if(battle_info){
         this.setData({
             battle_id   :battle_info.battle_id,
             player1_name:battle_info.player1_name,
             player2_name:battle_info.player2_name,
             event_stage :battle_info.event_stage,
             player1_score:wx.getStorageSync('player1_score'),
             player2_score:wx.getStorageSync('player2_score'),
             sum_frame:battle_info.sum_frame,
             last_time:Date.now()-1000,
             event_name:wx.getStorageSync('event_name')
         })

      }
  },

  toEditbattle:function(){
      if(this.remindAddBattleInfo()){
         wx.navigateTo({
            url: '/pages/editbattle/editbattle'
         })
      }
  },

  //选手1加分操作
  player1BonusPoint:function(){
      if(this.preventFastOperation()){
        var player1_score = Number(this.data.player1_score)+1;
        player1_score = player1_score > 99 ? 99 : player1_score;
        this.saveScore(1,player1_score);
      }
  },

  //选手1减分操作
  player1Reduction:function(){
      if(this.preventFastOperation()){
      var player1_score = Number(this.data.player1_score)-1;
      player1_score = player1_score < 0 ? 0 : player1_score;
      this.saveScore(1,player1_score);
      }
  },
  //选手2加分操作
  player2BonusPoint:function(){
      if(this.preventFastOperation()){
        var player2_score = Number(this.data.player2_score)+1;
        player2_score = player2_score > 99 ? 99 : player2_score;
        this.saveScore(2,player2_score);
      }
  
  },

  //选手2减分操作
  player2Reduction:function(){
      if(this.preventFastOperation()){
        var player2_score = Number(this.data.player2_score)-1;
        player2_score = player2_score < 0 ? 0 : player2_score;
        this.saveScore(2,player2_score);
      }
  },
  //修改比分
  saveScore:function(player,score){
      if(score < 10) {score = '00'+score};
      if(score > 9)  {score =  '0'+score};

      var that = this;
      //设置页面比分显示与修改本地缓存
      switch(player){
         case 1:this.setData({
             player1_score   : score,
         });wx.setStorage({
          key: 'player1_score',
          data: score
        });break;
         case 2:this.setData({
             player2_score   : score,
         });wx.setStorage({
          key: 'player2_score',
          data: score
        });break;
      }

      //修改远程比分
         var url  = getApp().data.servser+'/Battle/edit_score';

          wx.request({
            url: url,
            data: {
              'battle_id'              : that.data.battle_id,
              'player1_score'   : Number(that.data.player1_score),
              'player2_score'   : Number(that.data.player2_score),
            },
            header: {
                'content-type': 'application/json'
            },
            method : "GET",
            success: function(res) {
                
            }
          })

  },
  //防止用户操作过快
  preventFastOperation:function(){
      var that =this;
      var now_time = Date.now();
      if(this.remindAddBattleInfo()){
        if(now_time-this.data.last_time>1000){
            this.data.last_time = now_time;
            return true;
        }else{
            that.setData({
                item: {
                    display    :'show',
                    error_info :'操作太过频繁'
                }
            })
            this.closeErrorInfo();
            return false;
        }          
      }else{
          return false;
      } 
   },

    //定时器关闭提示信息
    closeErrorInfo:function(){
        var that = this;
        setTimeout(function(){
          that.setData({
              item: {display:'hide'}
              })
        },'1000')
    },
    //如果对局信息为空提示用户添加对局信息
    remindAddBattleInfo:function(){
        var that =this;
        var battle_info = wx.getStorageSync('battle_info');
      
        if(!battle_info){
            that.setData({
              item: {
                 display    :'show',
                 error_info :'请添加对局信息'
              }
            })
            this.closeErrorInfo();
            return false; 
        }else{
            return true;
        }
    }
})
